/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2021-10-27     Administrator       the first version
 */

#include <rtthread.h>
#include <rtdevice.h>
#include <test/test.h>


#define THREAD_PRIORITY         25
#define THREAD_STACK_SIZE       512
#define THREAD_TIMESLICE        5

/* 指向信号量的指针 */
rt_sem_t sem_food;
/* 线程1入口 */
void thread1_entry(void* parameter)
{
    while (1)
    {
        /* 线程1第一次运行 */
        rt_kprintf("thread1 is run!\n");
        /* 释放一次信号量 */
        rt_sem_release(sem_food);
        /* 线程1第二次运行 */
        rt_kprintf("thread1 run again!\n");
        /* 线程1延时1秒 */
        rt_thread_delay(RT_TICK_PER_SECOND);
    }
}

/* 线程2入口 */
void thread2_entry(void* parameter)
{
    while (1)
    {
        /* 试图持有信号量，并永远等待直到持有到信号量 */
        rt_sem_take(sem_food, RT_WAITING_FOREVER);
        /* 线程2正在运行 */
        rt_kprintf("thread2 is run!\n");
    }
}

/* DEMO初始化函数 */
void demo_init(void)
{
    /* 指向线程控制块的指针 */
    rt_thread_t thread1_id, thread2_id;
    /* 创建一个信号量，初始值是0 */
    sem_food = rt_sem_create("sem_food", 0, RT_IPC_FLAG_PRIO);
    if (sem_food == RT_NULL)
    {
        rt_kprintf("sem created fail!\n");
        return ;
    }
    /* 创建线程1 */
    thread1_id = rt_thread_create("thread1",
                    thread1_entry, RT_NULL,/* 线程入口是thread1_entry, 参数RT_NULL */
                    THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE);
    if (thread1_id != RT_NULL)
        rt_thread_startup(thread1_id);
    /* 创建线程2 */
    thread2_id = rt_thread_create("thread2",
                    thread2_entry, RT_NULL,/* 线程入口是thread2_entry, 参数RT_NULL */
                    THREAD_STACK_SIZE, THREAD_PRIORITY - 1, THREAD_TIMESLICE);
    if (thread2_id != RT_NULL)
        rt_thread_startup(thread2_id);
}


//------------------------------------------------------------------------------------------
#define LED_PIN     69

void Init_Led(void)
{
    rt_pin_mode(LED_PIN, PIN_MODE_OUTPUT);
}

void entry_threadLedDemo(void *parameter)
{
    rt_uint8_t cnt = 10;

    Init_Led();

    cnt = 10;

    while(cnt--)
    {
        rt_pin_write(LED_PIN, !rt_pin_read(LED_PIN));
        rt_thread_mdelay(100);
    }

    while(1);

//    while (1)
//    {
////        LOG_D("Hello RT-Thread!");
////        rt_pin_write(LED_PIN, !rt_pin_read(LED_PIN));
////        rt_thread_mdelay(300);
//    }
}


void Init_LedDemo(void)
{
    rt_thread_t threadLed;

    if(RT_NULL != (threadLed = rt_thread_create(
            "threadLedDemo",
            entry_threadLedDemo,
            RT_NULL,
            THREAD_STACK_SIZE,
            THREAD_PRIORITY,
            THREAD_TIMESLICE)))
    {
        rt_thread_startup(threadLed);
    }
}

